import http from 'axios'

// config http  // 192.168.0.102:8080  'http://39.108.122.70:8089/demo/
let baseUrl = 'http://39.108.122.70:8089/game/'
if (process.env.NODE_ENV === 'development') {
  baseUrl = 'http://39.108.122.70:8089/game/'
} else if (process.env.NODE_ENV === 'production') {
  baseUrl = 'http://39.108.122.70:8089/game/'
}
http.defaults.baseURL = baseUrl

// http.defaults.timeout = 2500
http.defaults.headers.post['Content-Type'] = 'application/json'
// cache-control: "max-age=0, private, must-revalidate"
// http.defaults.headers.post['Cache-control'] = 'max-age=5'

// Add a request interceptor 解决ie下url带中文参数乱码问题
http.interceptors.request.use(function (config) {
  // Do something before request is sent
  if (config.method === 'get') {
    config.url = encodeURI(config.url)
  }
  return config
}, function (error) {
  // Do something with request error
  return Promise.reject(error)
})

// Add a response interceptor
http.interceptors.response.use(function (response) {
  // Do something with response data
  return response
}, function (error) {
  // Do something with response error
  if (error.response) {
    if (error.response.status === 400) {
      this.$message('数据错误')
    } else if (error.response.status === 401) {
      // define window.app=vm in main.js
      window.app.$router.push({name: 'Login'})
      this.$message('数据错误')
    } else if (error.response.status === 403) {
      this.$message('您没有操作权限哦')
    } else if (error.response.status === 404) {
      this.$message('对象不存在')
    } else if (error.response.status === 500) {
      this.$message('服务器发生了一些错误哦')
    }
  }
  return Promise.reject(error)
})

export default http
